<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改</title>
</head>
<body>
    <div class="container">
        <div style="text-align: right;"><?php print_r("欢迎登陆：{$_SESSION['loginUser']['su_nickname']}")?></div>
            <form action="./updata.php" method="post">
            <div>
                <label>表名: <input type="text" name="tablename" value=""></label>
            </div>
            <div>
                <label>需要修改的ID: <input type="text" name="updatename" value=""></label>
            </div>
            <?php 
            $tablename=$_REQUEST['tablename'];
            $updatename=(int)$_REQUEST['updatename'];
            require_once("./DButil.php");
            $sql="select * from {$tablename}";
            $resultArr= query(getConn(), $sql);
            if($resultArr['success']){
                $product=$resultArr['data'];
                $p=$product[0];
                    foreach($p as $key => $value){
                        if($key!='p_id'&& $key!='m_id'){
                            echo "<div><label>$key:<input type='text' name={$key}></label></div>";
                        }
                    }
                }                  
            ?>
            <div style="margin:10px 0 0 20px">
                <button>查询表头</button>
                <button>修改</button>
                <button onclick="window.open('./selectProducts.php')">查询</button>
            </form>
            <?php 
            $str="";
            foreach($_REQUEST as $key=>$value){
                if($value!=$tablename && $value!=$updatename && $value!=null ){
                    $str.=$key.' = '.$value.',';
                }
            }
            $str=mb_substr($str,0,(mb_strlen($str)-1));
            if($tablename=='t_product'){
                $sql="update {$tablename} set {$str} where p_id={$updatename}";
            }else{
                $sql="update {$tablename} set {$str} where m_id={$updatename}";
            }
            $resultArr=update(getConn(),$sql);
            echo "<br>";
            if($resultArr['success']){
                echo "修改成功";
            }
            ?>
    </div>
</body>
</html>